//
//  ConfigureView.m
//  ICE_Player
//
//  Created by xl on 11-6-20.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ConfigureView.h"
#import "Globalize.h"
#import "ICE_global_var.h"
#import "BGView.h"


@implementation ConfigureView

@synthesize return_btn;
@synthesize bg_logo;
@synthesize lbl_configure;
@synthesize lbl_rotation;
@synthesize lbl_subtitle;
@synthesize lbl_text_info;
@synthesize lbl_loop_mode;
@synthesize lbl_loop_mode_state;
@synthesize lbl_press_me;
@synthesize lbl_ice_player;
@synthesize lbl_web;
@synthesize lbl_email;
@synthesize swc_rotation;
@synthesize swc_subtitle;
@synthesize swc_text_info;
@synthesize loop_mode_btn;
@synthesize i_btn;


- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
	[self destroy_SubViews];
    [super dealloc];
}

- (void)create_SubViews:(UIViewController*)controller
{
	CREATE_ICE_VIEW(bg_logo, BG_LOGO, controller);
	CREATE_ICE_VIEW(return_btn, cfg_return_btn, controller);
    
    CREATE_ICE_VIEW(lbl_configure, ICE_Label_View, controller);
    CREATE_ICE_VIEW(lbl_rotation, ICE_Label_View, controller);
    CREATE_ICE_VIEW(lbl_subtitle, ICE_Label_View, controller);
    CREATE_ICE_VIEW(lbl_text_info, ICE_Label_View, controller);
    CREATE_ICE_VIEW(lbl_loop_mode, ICE_Label_View, controller);
    CREATE_ICE_VIEW(lbl_loop_mode_state, ICE_Label_View, controller);
    CREATE_ICE_VIEW(lbl_press_me, ICE_Hidable_Label_View, controller);
    CREATE_ICE_VIEW(lbl_ice_player, ICE_Label_View, controller);
    CREATE_ICE_VIEW(lbl_web, ICE_Label_View, controller);
    CREATE_ICE_VIEW(lbl_email, ICE_Label_View, controller);
    CREATE_ICE_VIEW(swc_rotation, ICE_Switch_View, controller);
    CREATE_ICE_VIEW(swc_subtitle, ICE_Switch_View, controller);
    CREATE_ICE_VIEW(swc_text_info, ICE_Switch_View, controller);
    CREATE_ICE_VIEW(loop_mode_btn, Player_Loop_Mode_Btn, controller);
    CREATE_ICE_VIEW(i_btn, ICE_Button_View, controller);
    
    lbl_configure.text = IGS(EG_STR_ID_CFG_CONFIG);
    lbl_rotation.text = IGS(EG_STR_ID_CFG_ROTATION);
    lbl_subtitle.text = IGS(EG_STR_ID_CFG_SUBTITLE);
    lbl_text_info.text = IGS(EG_STR_ID_CFG_TEXT_INFO);
    lbl_loop_mode.text = IGS(EG_STR_ID_CFG_LOOP_MODE);
    lbl_press_me.text = IGS(EG_STR_ID_CFG_PRESS_ME);
    lbl_web.text = IGS(EG_STR_ID_CFG_WEB);
    lbl_email.text = IGS(EG_STR_ID_CFG_EMAIL);
    
    [super create_SubViews:controller];
}

- (void)scale_to_fit_iPad
{
    UIView* top_view = self;
    UIResponder* pctl = nil;
    CGRect rect;
    CGPoint pnt;
    CGRect p_rect;
    CGRect last_rect;
    while (top_view != nil)
    {
        //go to top view
        pctl = [top_view nextResponder];
        if (pctl && [pctl isKindOfClass:[UIView class]])
        {
            top_view = (UIView*)pctl;
            continue;
        }
        //get controller
        if (pctl && [pctl isKindOfClass:[UIViewController class]])
        {
            {
                rect = ((UIViewController*)pctl).view.bounds;
                rect.origin.x = 0.f;
                rect.origin.y = 0.f;
                pnt.x = rect.origin.x + 0.5f * rect.size.width;
                pnt.y = rect.origin.y + 0.5f * rect.size.height;
                self.bounds = rect;
                self.center = pnt;
                p_rect = rect;
            }
            break;
        }
        break;
    }
    
    //
    rect = bg_logo.bounds;
    rect.origin.x = p_rect.size.width * 0.5f - 0.5 * rect.size.width;
    rect.origin.y = p_rect.size.height * 0.3f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    bg_logo.bounds = rect;
    bg_logo.center = pnt;
    //
    rect = lbl_configure.bounds;
    rect.origin.x = p_rect.size.width * 0.5f - 0.5 * rect.size.width;
    rect.origin.y = 20.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_configure.bounds = rect;
    lbl_configure.center = pnt;
    //
    rect = lbl_rotation.bounds;
    rect.origin.x = 80.f;
    rect.origin.y = 120.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_rotation.bounds = rect;
    lbl_rotation.center = pnt;
    last_rect = rect;
    //
    rect = lbl_subtitle.bounds;
    rect.origin.x = last_rect.origin.x;
    rect.origin.y = last_rect.origin.y + last_rect.size.height + 10.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_subtitle.bounds = rect;
    lbl_subtitle.center = pnt;
    last_rect = rect;
    //
    rect = lbl_text_info.bounds;
    rect.origin.x = last_rect.origin.x;
    rect.origin.y = last_rect.origin.y + last_rect.size.height + 10.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_text_info.bounds = rect;
    lbl_text_info.center = pnt;
    last_rect = rect;
    //
    rect = lbl_loop_mode.bounds;
    rect.origin.x = last_rect.origin.x;
    rect.origin.y = last_rect.origin.y + last_rect.size.height + 10.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_loop_mode.bounds = rect;
    lbl_loop_mode.center = pnt;
    last_rect = rect;
    //
    rect = lbl_loop_mode_state.bounds;
    rect.origin.x = last_rect.origin.x;
    rect.origin.y = last_rect.origin.y + last_rect.size.height;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_loop_mode_state.bounds = rect;
    lbl_loop_mode_state.center = pnt;
    last_rect = rect;
    //
    last_rect = lbl_rotation.bounds;
    rect = swc_rotation.bounds;
    rect.origin.x = p_rect.size.width - rect.size.width - 80.f;
    rect.origin.y = last_rect.origin.y;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    swc_rotation.frame = rect;
    swc_rotation.center = pnt;
    //
    last_rect = lbl_subtitle.bounds;
    rect = swc_subtitle.bounds;
    rect.origin.x = p_rect.size.width - rect.size.width - 80.f;
    rect.origin.y = last_rect.origin.y;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    swc_subtitle.frame = rect;
    swc_subtitle.center = pnt;
    //
    last_rect = lbl_text_info.bounds;
    rect = swc_text_info.bounds;
    rect.origin.x = p_rect.size.width - rect.size.width - 80.f;
    rect.origin.y = last_rect.origin.y;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    swc_text_info.frame = rect;
    swc_text_info.center = pnt;
    //
    last_rect = lbl_loop_mode.bounds;
    rect = loop_mode_btn.bounds;
    rect.origin.x = p_rect.size.width - rect.size.width - 80.f;
    rect.origin.y = last_rect.origin.y;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    loop_mode_btn.bounds = rect;
    loop_mode_btn.center = pnt;
    //
    last_rect = lbl_loop_mode_state.bounds;
    rect = lbl_press_me.bounds;
    rect.origin.x = p_rect.size.width - rect.size.width - 80.f;
    rect.origin.y = last_rect.origin.y + 20.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_press_me.bounds = rect;
    lbl_press_me.center = pnt;
    //
    rect = lbl_ice_player.bounds;
    rect.origin.x = p_rect.size.width * 0.5f - 0.5f * rect.size.width;
    rect.origin.y = p_rect.size.height - 120.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_ice_player.bounds = rect;
    lbl_ice_player.center = pnt;
    last_rect = rect;
    //
    rect = lbl_web.bounds;
    rect.origin.x = p_rect.size.width * 0.5f - 0.5f * rect.size.width;
    rect.origin.y = last_rect.origin.y + last_rect.size.height;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_web.bounds = rect;
    lbl_web.center = pnt;
    last_rect = rect;
    //
    rect = lbl_email.bounds;
    rect.origin.x = p_rect.size.width * 0.5f - 0.5f * rect.size.width;
    rect.origin.y = last_rect.origin.y + last_rect.size.height;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    lbl_email.bounds = rect;
    lbl_email.center = pnt;
    last_rect = rect;
    //
    rect = i_btn.bounds;
    rect.origin.x = p_rect.size.width - rect.size.width - 80.f;
    rect.origin.y = p_rect.size.height - 120.f;
    pnt.x = rect.origin.x + 0.5f * rect.size.width;
    pnt.y = rect.origin.y + 0.5f * rect.size.height;
    i_btn.bounds = rect;
    i_btn.center = pnt;
    //
    
    self.alpha = 0.6f;
}

- (void) setHidden:(BOOL)enabled
{
	if (enabled == super.hidden)
	{
		return;
	}
	
	if (!enabled)
	{
        swc_rotation.on = !gf_get_ice_cfg()->is_rotation_locked;
        swc_subtitle.on = gf_get_ice_cfg()->enable_subtitle;
        swc_text_info.on = gf_get_ice_cfg()->enable_text_info;
        E_LOOP_MODE lm = gf_get_ice_cfg()->loop_mode;
        switch (lm)
        {
            case E_LM_NO_LOOP:
                loop_mode_btn.state = E_PLM_NO_LOOP;
                lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_NO_LOOP);
                [BGView get_Last_BG_view].player_view_controller.p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_NO_LOOP);
                break;
            case E_LM_SINGLE_LOOP:
                loop_mode_btn.state = E_PLM_SINGLE_LOOP;
                lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_SINGLE_LOOP);
                [BGView get_Last_BG_view].player_view_controller.p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_SINGLE_LOOP);
                break;
            case E_LM_LIST_LOOP:
                loop_mode_btn.state = E_PLM_LIST_LOOP;
                lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_LIST_LOOP);
                [BGView get_Last_BG_view].player_view_controller.p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_LIST_LOOP);
                break;
            case E_LM_RANDOM:
                loop_mode_btn.state = E_PLM_RANDOM;
                lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_RANDOM);
                [BGView get_Last_BG_view].player_view_controller.p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_RANDOM);
                break;
            default:
                break;
        }
		CATransition *animation = [CATransition animation];
		animation.delegate = self;
		animation.duration = 0.4f;
		animation.timingFunction = UIViewAnimationCurveEaseInOut;
		animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
		animation.type = kCATransitionMoveIn;//@"rippleEffect";//110
		animation.subtype = kCATransitionFromBottom;//kCATransitionFromTop;
		//animation.endProgress = 1.0;
		//animation.removedOnCompletion = NO;
		
		//[self.layer addAnimation:animation forKey:@"animation"];
		[self.layer addAnimation:animation forKey:@"animation"];
		
		[NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(On_Display_Controls) userInfo:nil repeats:NO];
	}
	else
	{
		[self On_Hide_Controls];
		CATransition *animation = [CATransition animation];
		animation.delegate = self;
		animation.duration = 0.4f;
		animation.timingFunction = UIViewAnimationCurveEaseInOut;
		animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
		animation.type = kCATransitionReveal;//@"rippleEffect";//110
		animation.subtype = kCATransitionFromTop;//kCATransitionFromBottom;//
		//animation.endProgress = 1.0;
		//animation.removedOnCompletion = NO;
		
		//[self.layer addAnimation:animation forKey:@"animation"];
		[self.layer addAnimation:animation forKey:@"animation"];
		
		//[NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(On_Display_Controls) userInfo:nil repeats:NO];
	}

	super.hidden = enabled;
}

- (void)On_Display_Controls
{
	bg_logo.hidden = FALSE;
	return_btn.hidden = FALSE;
    
    lbl_configure.hidden = FALSE;
    lbl_rotation.hidden = FALSE;
    lbl_subtitle.hidden = FALSE;
    lbl_text_info.hidden = FALSE;
    
    lbl_ice_player.hidden = FALSE;
    lbl_web.hidden = FALSE;
    lbl_email.hidden = FALSE;
    swc_rotation.hidden = FALSE;
    swc_subtitle.hidden = FALSE;
    swc_text_info.hidden = FALSE;
    i_btn.hidden = FALSE;
    
#if IS_LITE_VERSION
    loop_mode_btn.hidden = YES;
    lbl_loop_mode.hidden = YES;
    lbl_loop_mode_state.hidden = YES;
    lbl_press_me.hidden = YES;
#else
    loop_mode_btn.hidden = FALSE;
    lbl_loop_mode.hidden = FALSE;
    lbl_loop_mode_state.hidden = FALSE;
    lbl_press_me.hidden = FALSE;
#endif
}

- (void)On_Hide_Controls
{
	bg_logo.hidden = TRUE;
	return_btn.hidden = TRUE;
    
    lbl_configure.hidden = TRUE;
    lbl_rotation.hidden = TRUE;
    lbl_subtitle.hidden = TRUE;
    lbl_text_info.hidden = TRUE;
    lbl_loop_mode.hidden = TRUE;
    lbl_loop_mode_state.hidden = TRUE;
    lbl_press_me.hidden = TRUE;
    lbl_ice_player.hidden = TRUE;
    lbl_web.hidden = TRUE;
    lbl_email.hidden = TRUE;
    swc_rotation.hidden = TRUE;
    swc_subtitle.hidden = TRUE;
    swc_text_info.hidden = TRUE;
    loop_mode_btn.hidden = TRUE;
    i_btn.hidden = TRUE;
}

- (void)destroy_SubViews
{
	RELEASE_ICE_VIEW(bg_logo);
	RELEASE_ICE_VIEW(return_btn);
    
    RELEASE_ICE_VIEW(lbl_configure);
    RELEASE_ICE_VIEW(lbl_rotation);
    RELEASE_ICE_VIEW(lbl_subtitle);
    RELEASE_ICE_VIEW(lbl_text_info);
    RELEASE_ICE_VIEW(lbl_loop_mode);
    RELEASE_ICE_VIEW(lbl_loop_mode_state);
    RELEASE_ICE_VIEW(lbl_press_me);
    RELEASE_ICE_VIEW(lbl_ice_player);
    RELEASE_ICE_VIEW(lbl_web);
    RELEASE_ICE_VIEW(lbl_email);
    RELEASE_ICE_VIEW(swc_rotation);
    RELEASE_ICE_VIEW(swc_subtitle);
    RELEASE_ICE_VIEW(swc_text_info);
    RELEASE_ICE_VIEW(loop_mode_btn);
    RELEASE_ICE_VIEW(i_btn);
}


@end
